package com.xjc.business.controller.schadmin;

import com.xjc.business.entity.po.NewsPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.NewsVo;
import com.xjc.business.service.SchoolNewsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 徐锦程
 * 时间:2025-02-04 16:01
 */
@RestController
@RequestMapping("/sch_admin/news")
public class SchNewsController {

    @Autowired
    SchoolNewsService schoolNewsService;

    /**
     * 分页查询
     * @param pageSize 每页大小
     * @param currentPage 当前页码
     * @param title 标题
     * @param schoolAdminId 学院管理员id
     * @return 数据
     */
    @RequestMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<NewsVo>> getNewsPage(
            @PathVariable(value = "pageSize",required = false) Integer pageSize,
            @PathVariable(value = "currentPage",required = false) Integer currentPage,
            String title,
            Integer schoolAdminId
    ){
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }
        if(schoolAdminId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return schoolNewsService.getNewsPage(pageSize,currentPage,title,schoolAdminId);
    }

    /**
     * 获取新闻内容
     * @param id 新闻id
     * @return 内容
     */
    @RequestMapping("/content/{id}")
    public Result<String> getNewsContent(@PathVariable(value = "id",required = false) Integer id){
        return Result.okWithData(schoolNewsService.getById(id).getContent());
    }

    /**
     * 添加新闻
     * @param newsPo 新闻内容
     * @return 结果
     */
    @PostMapping("/add")
    public Result<String> addNews(@RequestBody(required = false) NewsPo newsPo){
        if(newsPo == null || newsPo.getPulishId() == null || newsPo.getType() == null || newsPo.getTitle() == null || newsPo.getContent() == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return schoolNewsService.addNews(newsPo);
    }

    /**
     * 阅读新闻
     * @param id 新闻id
     * @return 结果
     */
    @PostMapping("/read/{id}")
    public Result<String> readNews(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return schoolNewsService.readNews(id);
    }
}